package com.chenjiahao.kaiyanwan.ui.discovery

import android.app.Activity
import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.chenjiahao.kaiyanwan.*
import com.chenjiahao.kaiyanwan.base.CommonAdapter
import com.chenjiahao.kaiyanwan.base.ViewHolder
import com.chenjiahao.kaiyanwan.ui.videodetail.VideoDetailActivity
import com.hazz.kotlinmvp.mvp.model.bean.CategoryBean

/**
 * Created by chenjh on 2020/9/17
 */
class CategoryAdapter(mContext: Context, categoryList: ArrayList<CategoryBean>, layoutId: Int) :
    CommonAdapter<CategoryBean>(mContext, categoryList, layoutId) {


    private var textTypeface: Typeface? = null

    init {
        textTypeface = Typeface.createFromAsset(
            KaiYanApplication.context.assets,
            "fonts/FZLanTingHeiS-L-GB-Regular.TTF"
        )
    }

    /**
     * 设置新数据
     */
    fun setData(categoryList: ArrayList<CategoryBean>) {
        mData.clear()
        mData = categoryList
        notifyDataSetChanged()
    }

    /**
     * 绑定数据
     */
    override fun bindData(holder: ViewHolder, data: CategoryBean, position: Int) {
        holder.setText(R.id.tv_category_name, "#${data.name}")
        //设置方正兰亭细黑简体
        holder.getView<TextView>(R.id.tv_category_name).typeface = textTypeface

        holder.setImagePath(
            R.id.iv_category,
            object : ViewHolder.HolderImageLoader(data.bgPicture) {
                override fun loadImage(iv: ImageView, path: String) {
                    Glide.with(mContext)
                        .load(path)
                        .placeholder(R.color.color_darker_gray)
                        .transition(DrawableTransitionOptions().crossFade())
                        .thumbnail(0.5f)
                        .into(iv)
                }
            })

        holder.setOnItemClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                if (v != null) {
                    startActivity<CategoryDetailActivity>(mContext as Activity, v) {
                        putExtra(BUNDLE_CATEGORY_DATA, data)
                    }
                }
            }

        })

    }

}
